package cn.kgc.springbootdemo.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.springbootdemo.mapper.MenuMapper;
import cn.kgc.springbootdemo.entity.Menu;
import cn.kgc.springbootdemo.service.MenuService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 菜单表(Menu)表服务实现类
 *
 * @author makejava
 * @since 2021-03-31 17:10:40
 */
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    /**
     *  树状排序
     *
     * @return
     */
    @Override
    public List<Tree<String>> getTree() {
        //TODO 获取所有数据
        List<Menu> menus = baseMapper.selectList(Wrappers.<Menu>lambdaQuery().orderByAsc(Menu::getId).orderByAsc(Menu::getOrderNum));

        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setWeightKey("menuKey");
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setDeep(5);

        List<Tree<String>> build = TreeUtil.build(menus, "0", treeNodeConfig,
                (treeMenus, tree) -> {
                    tree.setId(treeMenus.getId().toString());
                    tree.setParentId(treeMenus.getParentId().toString());
                    tree.setWeight(treeMenus.getMenuKey());
                    tree.setName(treeMenus.getMenuName());
                    tree.putExtra("url",treeMenus.getUrl());
                    tree.putExtra("icon",treeMenus.getIcon());
                    tree.putExtra("type",treeMenus.getType());
                    tree.putExtra("orderNum",treeMenus.getOrderNum());
                });
        return build;
    }
}
